package com.example.mobifin_busticket.views.adapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.example.mobifin_busticket.R;
import com.example.mobifin_busticket.models.BusModel;
import com.example.mobifin_busticket.models.FareModel;
import com.example.mobifin_busticket.models.SourceModel;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CustomListAdapterForTrips extends BaseAdapter {
	class ViewHolder {

		TextView tvTravelName, tvBusTime, tvBusType, tvTravelTime, tvSeat,
				tvRate;
	}

	ArrayList<BusModel> busList;
	private Context context;
	List<BusModel> busData = null;
	LayoutInflater vi;
	String stringNA = "NA", totalHour = "", departureTime = "",
			arraivalTime = "";
	DateFormat avformat;

	public CustomListAdapterForTrips(Context context,
			ArrayList<BusModel> BusList) {
		this.context = context;
		this.busData = BusList;
		vi = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		avformat = new SimpleDateFormat("yyyy-MM-dd hh:mm a");
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return busData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return busData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		System.out.println("In Get View==>position:" + position);

		View v = convertView;

		if (null == v) {
			holder = new ViewHolder();
			v = vi.inflate(R.layout.row_bus_search, null);
			holder.tvTravelName = (TextView) v
					.findViewById(R.id.bus_travel_name);

			holder.tvBusTime = (TextView) v.findViewById(R.id.txt_bustime);
			holder.tvBusType = (TextView) v
					.findViewById(R.id.row_busList_busType);
			holder.tvTravelTime = (TextView) v.findViewById(R.id.traveltime);
			holder.tvSeat = (TextView) v.findViewById(R.id.bus_no_seats);
			holder.tvRate = (TextView) v.findViewById(R.id.bus_fare_rs);

			v.setTag(holder);

		} else {
			holder = (ViewHolder) v.getTag();
		}
		
		arraivalTime = busData.get(position).getarrivalTime();
		departureTime = busData.get(position).getdepartureTime();
		
		getAdapterArrivalTimewithFormat();
		getDepartureTimewithFormat();
		getDiffrencebetweentwoTimes(position);
		System.out.println("Total hour:" + totalHour);
		
		holder.tvTravelName.setText(busData.get(position).gettravels());
		holder.tvTravelTime.setText("" + totalHour + "" + " Hours");
		holder.tvBusType.setText(busData.get(position).getbusType());
		holder.tvBusTime.setText(departureTime + "-" + arraivalTime);
		holder.tvSeat.setText("" + busData.get(position).getavailableSeats()
				+ " Seats");

		ArrayList<FareModel> farelist = busData.get(position).getFareList();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < farelist.size(); i++) {
			if (i != farelist.size() - 1) {
				sb.append(farelist.get(i).getfare() + "/");
			} else {
				sb.append(farelist.get(i).getfare());
			}
		}
		holder.tvRate.setText("INR:" + sb);
		return v;

	}

	private void getDepartureTimewithFormat() {

		Date departDate = null;
		try {
			departDate = avformat.parse("" + departureTime);
			DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
			departureTime = dateFormat.format(departDate).toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	private void getAdapterArrivalTimewithFormat() {

		Date avDate = null;
		try {
			avDate = avformat.parse("" + arraivalTime);
			DateFormat avdateFormat = new SimpleDateFormat("hh:mm a");
			arraivalTime = avdateFormat.format(avDate).toString();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	// Get difference Between Two Times to get No of Hours difference
	private void getDiffrencebetweentwoTimes(int position) {
		Date end = null, start = null;
		try {

			end = avformat.parse("" + busData.get(position).getarrivalTime());
			start = avformat.parse(""
					+ busData.get(position).getdepartureTime());
			long difference = Math.abs(end.getTime() - start.getTime());

			int minutes = (int) ((difference / (1000 * 60)) % 60);
			int hours = (int) ((difference / (1000 * 60 * 60)) % 24);

			totalHour = String.valueOf(hours + "." + minutes);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
